/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.meanzoft.m3d;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.meanzoft.m3d.scene.camera.Camera;

/**
 *
 * @author Meanz
 */
public class Viewport {

    private Camera camera;
    private boolean isCreated;
    private int width, height;

    public void create() {
        try {
            Display.create();
            Display.setTitle("M3DEngine");
            isCreated = true;
        } catch (LWJGLException ex) {
            ex.printStackTrace();
            return;
        }
    }

    public void setTitle(String text) {
        Display.setTitle(text);
    }

    public DisplayMode[] createDisplayMode(int minWidth, int minHeight, int maxWidth,
            int maxHeight, int minBPP, int maxBPP, int minFreq, int maxFreq) {
        try {
            DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(
                    minWidth, minHeight, maxWidth, maxHeight, minBPP, maxBPP,
                    minFreq, maxFreq);
            return dm;
        } catch (LWJGLException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public DisplayMode[] createDisplayMode(int minWidth, int minHeight) {
        return createDisplayMode(minWidth, minHeight, -1, -1, -1, -1, 60, 60);
    }

    public void setDisplayMode(int width, int height) {
        try {
            this.width = width;
            this.height = height;
            DisplayMode[] dm = createDisplayMode(width, height);
            org.lwjgl.util.Display.setDisplayMode(dm,
                    new String[]{
                        "width=" + width,
                        "height=" + height,
                        "freq=" + 60,
                        "bpp=" + org.lwjgl.opengl.Display.getDisplayMode().getBitsPerPixel()
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getWidth() {
        return Display.getDisplayMode().getWidth();
    }

    public int getHeight() {
        return Display.getDisplayMode().getHeight();
    }

    public void destroy() {
        if (isCreated) {
            Display.destroy();
        }
    }

    public void setCamera(Camera camera) {
        if (camera == null) {
            return;
        }
        this.camera = camera;
    }

    public Camera getCamera() {
        return camera;
    }

    public void refresh() {
        Display.update();
        // Display.sync(60);
    }
}
